package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> findAll(EmpQueryParam param) {
        //1. 设置分页参数(MyBatis-Plus)
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        //2. 执行查询 只有分页条件，没有查询条件
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getName() != null, Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd());
        //2. 执行查询 只有分页条件，没有查询条件
        page = page(page, queryWrapper);
        // 获取员工集合
        //处理部门名称展示
        List<Emp> emps = page.getRecords();
        emps.forEach(emp -> {
            Dept dept = deptService.getById(emp.getDeptId());
            if (dept != null) {
                emp.setDeptName(dept.getName());
            }
        });

        //3. 封装查询结果
        return new PageResult<>(page.getTotal(), emps);
    }

        /**
         * 新增员工
         *
         *
         * @param emp 员工信息
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveEmp(Emp emp) {
            // 调用service层方法保存员工信息
            // 补全基础属性
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());

            save(emp);

            // 调用service层方法保存员工工作经历信息
            List<EmpExpr> exprList = emp.getExprList();

            // 添加工作经历所属员工
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
            empExprService.saveBatch(exprList);
        }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));


        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && exprList.size() > 0){
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
    }

    @Override
    public void deletByids(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));

    }
}

